import {Component,OnInit,ViewChild} from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
import { ActivatedRoute } from '@angular/router';
declare var $:any;
@Component({
  selector: 'doormodel',
  templateUrl: './doormodel.component.html',
  styleUrls: ['./doormodel.component.css'],
  animations: [
      flyIn
  ]
})
export class Doormodel implements OnInit{  
  @ViewChild('myModal') public myModal:ModalDirective;
  proData:any;
  proname:any;
  prodes:any;
  product_name:any;
  product_description:any;
  door_number:any;
  productdescription:any;
  product_id:any; 
  doordata = [];
  btype:number;
  ydoordata:any;
  constructor(private myHttp:MyHttp,private activeRoute: ActivatedRoute) {}
  ngOnInit() {   
    //this.getdata();
    this.activeRoute.params.subscribe(
      params=>{
        this.product_id = params['productId'];        
        //console.log(this.deviceId)  
        this.getoneproduct();  
        this.getdata();
      
       
        //this.http.get('assets/diqu.json').subscribe(response =>this.division = response.json());
      }

    ); 
  }

  cancel(){
    this.door_number="";
   
  }

  comfirmnumber(){    
    let onedoor = {"dna" :[],"dsz" :[],"dcg" :[]};
    for(let i=0;i<this.door_number ;i++){ 
      let dna = 'door_name' + i;
      onedoor.dna[i] = dna;  
      let dsz = 'door_size' + i;
      onedoor.dsz[i] = dsz;
      let dcg = 'door_charge'+ i;
      onedoor.dcg[i] = dcg;
      this.doordata.push(onedoor);
    }
   

    this.myModal.hide();   

  }


  savealldoor(data){
    let ttdata = [];
    let tsdata = {"door_name":'',"door_size" :'',"door_charge":''};
    let num = this.doordata.length - 1;
    let hum = this.doordata[num].dcg.length;
    for(let i=0;i<hum;i++){
      tsdata.door_name = this.doordata[num].dna[i];
      tsdata.door_size = this.doordata[num].dsz[i]; 
      tsdata.door_charge = this.doordata[num].dcg[i]; 
      ttdata.push(tsdata) ;  
      tsdata = {"door_name":'',"door_size" :'',"door_charge":''};
    }
    this.myHttp.post('template/save',{
        "product_id":this.product_id,
        "data":ttdata
    }).subscribe(res=>{       
      if(res.errorCode==0){
        alert(res.message);
        this.getdata();        
      }   
      
    })
    
  }

  getdata(){
    this.myHttp.post('template/findByProductId',{
        "product_id":this.product_id  
    }).subscribe(res=>{       
      if(res.errorCode==0){
        //console.log(res.message);
        this.ydoordata = res.message;
         //console.log(this.proData)
      }   
      
    })
  }


  getoneproduct(){
    this.myHttp.post('product/findById',{
        "product_id":this.product_id
    }).subscribe(res=>{       
      if(res.errorCode==0){
        this.proData = res.message;
         //console.log(this.proData)
      }   
      
    })
  }



}

